{% extends "registration/login.html" %}
{% load i18n %}
{% block sub_title %}{% trans "Signup" %} - {% endblock %}

{% block extra_style %}{{block.super}}
<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}bootstrap/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}bootstrap/bootstrap.popover.min.css" />
{% endblock %}

{% block main_content %}
<div class="login-panel-outer-container vh" id="registration-form">
  {% if request.user.is_authenticated %}
  <div class="login-panel" id="log-in-panel">
    <h1 class="login-panel-hd">{% trans "Signup" %}</h1>
    <div class="reg-logged-panel">
      <span class="loading-icon loading-tip"></span>
      <p class="m-0">{% trans "Welcome back, you are already signed in." %}</p>
      <p>{% trans "You will be redirected to the home page within 5 seconds." %}</p>
    </div>
  </div>

  {% else %}
  <div class="login-panel">
    <h1 class="login-panel-hd">{% trans "Signup" %}</h1>
    <form action="" method="post" id="signup-form">{% csrf_token %}
      {% if form.name.field.required %}
      <label for="id_name">{% trans "Name" %}</label>
      {{ form.name }} {{ form.name.errors }}
      {% endif %}

      <label for="id_email">{% trans "Email" %}</label>
      {{ form.email }}            {{ form.email.errors }}
      <label for="id_password1">{% trans "Password" %}</label>
      <div class="position-relative" id="password-input1">
        {{ form.password1 }}        {{ form.password1.errors }}
      </div>
      <div id="pwd_strength"></div>
      <label for="id_password2">{% trans "Confirm Password" %}</label>
      <div class="position-relative" id="password-input2">
        {{ form.password2 }}        {{ form.password2.errors }}
      </div>

      {% if form.department.field.required %}
      <label for="id_department">{% trans "Department" %}</label>
      {{ form.department }}       {{ form.department.errors }}
      {% endif %}

      {% if form.telephone.field.required %}
      <label for="id_telephone">{% trans "Telephone" %}</label>
      {{ form.telephone }}        {{ form.telephone.errors }}
      {% endif %}

      {% if form.note.field.required %}
      <label for="id_note">{% trans "Note" %}</label>
      {{ form.note }}             {{ form.note.errors }}
      {% endif %}

      <p class="error hide"></p>
      <button type="submit" class="submit btn btn-primary btn-block">{% trans "Sign Up" %}</button>
    </form>
    <div class="login-panel-bottom-container">
      {# language will be shown here #}
    </div>
    <div>
      {% include "snippets/policy_service_link.html" %}
    </div>
  </div>
  {% endif %}
</div>
{% endblock %}

{% block extra_script %}{{ block.super }}
<script type="text/javascript" src="{{MEDIA_URL}}bootstrap/bootstrap.popover.min.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}bootstrap/bootstrap.min.js"></script>
<script type="text/javascript">
  {% if request.user.is_authenticated %}
  setTimeout(function() {
    location.href = '{{SITE_ROOT}}';
  }, 5000);

  {% else %}
  {% include "snippets/password_strength_js.html" %}
  $(function () {
    setupPasswordField("id_password1", passwd_tip, template);
  });
  $('#signup-form').on('submit', function() {
    var email = $('input[name="email"]').val().trim(),
      pwd1 = $('input[name="password1"]').val().trim(),
      pwd2 = $('input[name="password2"]').val().trim();

    if (!email) {
      $('.error').html("{% trans "Email cannot be blank" %}").removeClass('hide');
      return false;
    }
    if (!pwd1) {
      $('.error').html("{% trans "Password cannot be blank" %}").removeClass('hide');
      return false;
    }
    if (!pwd2) {
      $('.error').html("{% trans "Please enter the password again" %}").removeClass('hide');
      return false;
    }
    if (pwd1 != pwd2) {
      $('.error').html("{% trans "Passwords don't match" %}").removeClass('hide');
      return false;
    }
    if (!checkPasswordStrength(pwd1, {{strong_pwd_required}})) {
      $('.error').html(passwd_tip).removeClass('hide');
      return false;
    }
  });

  {% endif %}
</script>
{% endblock %}
